﻿using System;
using System.Collections.Generic;
using System.Text;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using DevExpress.Xpo;

namespace RemoteImaging.Test
{
    [TestFixture]
    public class CarCountUpdaterTest
    {
        [SetUp]
        public void Init()
        {
            Helper.InitXpo();
        }

        [Test]
        public void Test()
        {
            //
            // TODO: Add test logic here
            //

            var updater = new RemoteImaging.LicensePlate.CarCountUpdater();
            updater.CountToUpdate = 1;

            var time = DateTime.Now;
            var lpi = new LicensePlate.LicensePlateInfo() { CaptureTime = time };

            AddLpi(10, updater, lpi);

            XPCollection<LicensePlate.CarCountPerHour> collection = GetCollection();
            Assert.AreEqual(collection.Count, 1);

            var carCount = collection[0];
            Assert.IsTrue(carCount.Count == 10);
            Assert.IsTrue(carCount.Time.Minute == 0 && carCount.Time.Second == 0);

            AddLpi(1, updater, lpi);
            collection = GetCollection();
            Assert.AreEqual(collection.Count, 1);

            carCount = collection[0];
            Assert.IsTrue(carCount.Count == 11);

            lpi.CaptureTime.AddMinutes(1);
            AddLpi(1, updater, lpi);

            collection = new XPCollection<LicensePlate.CarCountPerHour>();
            Assert.AreEqual(collection.Count, 1);

            carCount = collection[0];
            Assert.AreEqual(carCount.Count, 12);
            Assert.IsTrue(carCount.Time.Minute == 0 && carCount.Time.Second == 0);


            lpi.CaptureTime = lpi.CaptureTime.AddHours(1);
            AddLpi(1, updater, lpi);

            collection = GetCollection();
            Assert.IsTrue(collection.Count == 2);

            carCount = LicensePlate.Helper.FindCarCountByTime(lpi.CaptureTime, new Session());
            Assert.AreEqual(carCount.Count, 1);
            Assert.IsTrue(carCount.Time.Minute == 0 && carCount.Time.Second == 0);
        }

        private static void AddLpi(int count, RemoteImaging.LicensePlate.CarCountUpdater updater, RemoteImaging.LicensePlate.LicensePlateInfo lpi)
        {
            for (int i = 0; i < count; i++)
            {
                updater.HandleCarCountByHour(new[] { lpi });
            }
        }
        private static XPCollection<LicensePlate.CarCountPerHour> GetCollection()
        {
            var collection = new XPCollection<LicensePlate.CarCountPerHour>();
            return collection;
        }
    }
}
